$.ready(function () {
    $(".button").bind('click',function(){
        if($(this).attr("v") == 2){
            $("input[name='Submit']").val($(this).attr("v"));
            confirmT();
        }else{
            $("input[name='Submit']").val($(this).attr("v"));
            $("#form1").submit();
        }
    });
    $(".datepicker").datepicker({ dateFormat: "yy-mm-dd" });
    $("#btnSubmit").bind("click",function(){
        return validateForm();

    });
    var p0Result = $("select[name='computer']").attr("disabled");
    var p1Result = $("input[name='domain_account']").attr("disabled");
    var p2Result = $("input[name='re_entry_date']").attr("disabled");
    var validateArray = new Array();
    if(!p0Result){
        var computer = $("select[name='computer']");
        var workSpace = $("select[name='work_space']");
        var seat = $("input[name='seat']");
        var computerCode = $("input[name='computer_code']");

        computer.bind("change",function(){

            var obj =$(this);
            if(!obj.val()){
                obj.css("borderColor","red");
                return;
            }
            obj.css("borderColor","#d3d3d4");
        });
        workSpace.bind("change",function(){
            var obj =$(this);
            if(!obj.val()){
                obj.css("borderColor","red");
                return;
            }
            obj.css("borderColor","#d3d3d4");
        });


        validateArray.push(computer);
        validateArray.push(workSpace);
        validateArray.push(seat);
        validateArray.push(computerCode);

        validate(computer);
        validate(workSpace);
        validate(seat);
        validate(computerCode);
    }
    if(!p1Result){
        var domainAccount = $("input[name='domain_account']");
        var email = $("input[name='office_mail']");
        //var crm = $("input[name='crm_account']");

        validateArray.push(domainAccount);
        validateArray.push(email);
        //validateArray.push(crm);
        validate(domainAccount);
        validate(email);
        //validate(crm);

    }
    if(!p2Result){
        var date = $("input[name='re_entry_date']");
        validateArray.push(date);
        validate(date);
    }

    function validate(obj){
        obj.bind('keyup',function(){
            var obj = $(this);
            if(!obj.val()){
                obj.css("borderColor","red");
                return;
            }
            if(obj.attr("name") == 'office_mail'){
                var regExp = new RegExp("^([a-zA-Z0-9]+[_|_|.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|_|.]?)*[a-zA-Z0-9]+\.(?:com|cn)$");
                var regResult = regExp.test(obj.val());
                if(!regResult){
                    obj.css("borderColor","red");
                    return;
                }
            }
            obj.css("borderColor","#d3d3d4");
        });
        obj.bind('blur',function(){
            var obj = $(this);
            if(!obj.val()){
                obj.css("borderColor","red");
                return;
            }
            if(obj.attr("name") == 'office_mail'){
                var regExp = new RegExp("^([a-zA-Z0-9]+[_|_|.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|_|.]?)*[a-zA-Z0-9]+\.(?:com|cn)$");
                var regResult = regExp.test(obj.val());
                if(!regResult){
                    obj.css("borderColor","red");
                    return;
                }
            }
            obj.css("borderColor","#d3d3d4");
        });
    }
    function validateForm(){
        var result = true;
        var firstObj = null;
        for(var i=0;i<validateArray.length;i++){
            var obj = $(validateArray[i]);
             if(!obj.val()){
                 obj.css("borderColor","red");
                 firstObj = firstObj ? firstObj:obj;
                 result = false;
             }else if(obj.attr("name") == 'office_mail'){
                     var regExp = new RegExp("^([a-zA-Z0-9]+[_|_|.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|_|.]?)*[a-zA-Z0-9]+\.(?:com|cn)$");
                     var regResult = regExp.test(obj.val());
                     if(!regResult){
                         obj.css("borderColor","red");
                         result = false;
                         firstObj = firstObj ? firstObj:obj;
                     }
                 }
             else{
                 obj.css("borderColor","#d3d3d4");
             }

         }
        if(firstObj){
            firstObj.focus();
        }
        return result;
    }
    function confirmT(){
        if(confirm("确定要取消？")){
            $("#form1").submit();
        }
    }
    function check(){

    }
}());
